//
//  AddMonitorViewController.m
//  SIPphone
//
//  Created by wrt on 14-4-15.
//  Copyright (c) 2014年 wrtsoft. All rights reserved.
//

#import "AddMonitorViewController.h"

@interface AddMonitorViewController ()

@end

@implementation AddMonitorViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = NSLocalizedString(@"Add monitoring device", @"添加监视设备 Add monitoring device");;
}
- (IBAction)backgroundTouchDown:(id)sender
{
    [self.nameTextField resignFirstResponder];
    [self.numberTextField resignFirstResponder];
}
- (IBAction)nameDidEndOnExit:(id)sender
{
    [self.nameTextField resignFirstResponder];
}
- (IBAction)numberDidEndOnExit:(id)sender
{
    [self.numberTextField resignFirstResponder];
}
- (IBAction)cancelButtonPressed:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)saveButtonPressed:(id)sender
{
    if (self.nameTextField.text.length > 0 && self.numberTextField.text.length > 0)
    {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSMutableArray *monitorArray = [[NSMutableArray alloc] init];
        monitorArray = [defaults mutableArrayValueForKey:@"SAVEMONITORNAME"];
        NSMutableArray *numberArray = [[NSMutableArray alloc] init];
        numberArray = [defaults mutableArrayValueForKey:@"SAVEMONITORNUMBER"];
        BOOL isExist = NO;
        if (monitorArray.count > 0 && numberArray.count > 0)
        {
            for (int i = 0 ; i < monitorArray.count ; i++)
            {
                if ([self.nameTextField.text isEqualToString:monitorArray[i]])
                {
                    if ([self.numberTextField.text isEqualToString:numberArray[i]])
                    {
                        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"notice", @"通知 notice") message:NSLocalizedString(@"The monitoring device already exists, do not re-add", @"已经存在该监视设备，请勿重新添加") delegate:self cancelButtonTitle:NSLocalizedString(@"OK", @"好的 OK") otherButtonTitles: nil];
                        [alert show];
                        isExist = YES;
                        break;
                    }
                }
            }
        }
        
        if (isExist == NO)
        {
            [monitorArray addObject:self.nameTextField.text];
            [numberArray addObject:self.numberTextField.text];
            [defaults setValue:monitorArray forKey:@"SAVEMONITORNAME"];
            [defaults setValue:numberArray forKey:@"SAVEMONITORNUMBER"];
            [defaults synchronize];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"notice", @"通知 notice") message:NSLocalizedString(@"please fill out the complete contents", @"请将内容填写完整 Please fill out the complete contents") delegate:self cancelButtonTitle:NSLocalizedString(@"OK", @"好的 OK") otherButtonTitles: nil];
        [alert show];
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
